package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:线程安全问题
 * User: bjc
 * Date: 2025-08-17
 * Time: 15:26
 */
public class ThreadDemo16 {
    private static int count = 0;

    public static void main(String[] args) throws InterruptedException {
        //创建两个线程，每个线程对count加5W次
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                count++;
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                count++;
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();
        //正确结果应该是10W
        System.out.println("count = " + count);
    }
}
